package ddd.ajava.chapter7.question1;

import java.beans.XMLEncoder;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

import com.thoughtworks.xstream.XStream;

public class ObjectToXML {
	public static void main(String[] args){
		Person person=new Person();
		person.setName("ddd");
		person.setAge(23);
		Address address=new Address("chonqqing","Hong Guang Street");
		person.setAddress(address);
		ObjectToXML.writeXML(person);
	}
	public static void writeXML(Object obj){
		try{
			String packageName=ObjectToXML.class.getPackage().getName().replace(".","\\");
			BufferedWriter bw=new BufferedWriter(new FileWriter("src\\"+packageName+"\\student.xml",true));
			XStream xstream=new XStream();
			String serialvalue=xstream.toXML(obj);
			bw.newLine();
			bw.write(serialvalue);
			bw.newLine();
			bw.close();
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
}
